//
//  SessionCell.m
//  FKGW
//
//  Created by song ce on 2017/2/17.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "SessionCell.h"
#import "Tools.h"
#import "OSSHelper.h"
#import "UIView+Extend.h"


@implementation SessionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.userIcon.layer.cornerRadius = 24;
    self.userIcon.layer.masksToBounds = YES;
}
-(void)setModel:(EaseConversationModel *)model{
    _model = model;
    self.lastMessageLabel.text = _model.lastMessage;
    self.lastTimeLabel.text = _model.lastTime;

    int unReadCount = _model.conversation.unreadMessagesCount;
    if(unReadCount>0){
        self.countBtn.hidden = NO;
        NSString *uc = [NSString stringWithFormat:@"%d",unReadCount];
        if(unReadCount>99){
           uc = [NSString stringWithFormat:@"%@",@"99+"];
        self.countBtn.width = 22;
        }
        [self.countBtn setTitle:uc forState:UIControlStateNormal];
    }else{
        self.countBtn.hidden = YES;
    }
    NSString *uid = _model.conversation.conversationId;
    [self loadUserInfoWithUid:uid];
}

-(void)loadUserInfoWithUid:(NSString*)userId{
    if(!userId.length){
        return;
    }
//     _nameLabel.text = userId;
    NSDictionary  *huanxinDic =[[NSUserDefaults standardUserDefaults]objectForKey:userId];
    if(huanxinDic){
        _nameLabel.text = huanxinDic[@"nickName"];
        NSData *imageData = huanxinDic[@"pic"];
        self.userIcon.image = [UIImage imageWithData:imageData];
    }else{
        NSMutableArray *userNames = [NSMutableArray arrayWithObject:userId];
        NSData *pramasData = [NSJSONSerialization dataWithJSONObject:userNames options:NSJSONWritingPrettyPrinted error:nil];
        NSString *userName = [[NSString alloc]initWithData:pramasData encoding:NSUTF8StringEncoding];
        NSDictionary *paramDic = @{@"userName":userName};
        //2.68获取聊天对象信息接口
        __weak typeof(self) weakSelf = self;
        [Tools AFNetworkingRequestTypePostWithUrl:GETCHATOBJECTSINFORMATION paramters:paramDic afterResultBlock:^(NSDictionary *resultDic) {
            DLog(@"//获取聊天对象信息接口----%@",resultDic);
            if([resultDic[@"flag"] isEqualToString:@"true"]){
                NSArray *listArr = resultDic[@"data"];
                if([listArr isKindOfClass:[NSNull class]]){
                    return ;
                }
                NSDictionary *dic = listArr.lastObject;
                NSString *nickName =dic[@"nickName"];
                dispatch_async(dispatch_get_main_queue(), ^{
                  _nameLabel.text = nickName;
               });
                //缓存到本地
                [[OSSHelper sharedHelper] OSSDownLoadWithObjectKey:dic[@"pic"]xOssProcess:[NSString stringWithFormat:@"image/resize,m_fill,w_%d,h_%d",48,48]  downLoadBlock:^(NSData *data) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        weakSelf.userIcon.image = [UIImage imageWithData:data];
                        NSDictionary *dic = @{@"pic":data,@"nickName":nickName};
                        [[NSUserDefaults standardUserDefaults]setObject:dic forKey:userId];
                        [[NSUserDefaults standardUserDefaults] synchronize];
                    });
             }];
            }
        }];
    }
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
